package com.cloudfun.campusshare.controller;

import com.cloudfun.campusshare.common.constant.AuthType;
import com.cloudfun.campusshare.common.constant.RoleType;
import com.cloudfun.campusshare.common.exception.BusinessException;
import com.cloudfun.campusshare.common.model.vo.AppEditVO;
import com.cloudfun.campusshare.common.model.vo.AppTokenVO;
import com.cloudfun.campusshare.common.model.vo.AppVO;
import com.cloudfun.campusshare.common.response.BaseResult;
import com.cloudfun.campusshare.common.security.AuthRequired;
import com.cloudfun.campusshare.service.AppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.groups.Default;

/**
 * @author linyangping
 * @date 2019/11/7
 */
@RestController
@RequestMapping("/v1/app")
@Validated
@Api(value = "应用模块", description = "应用模块", position = 2)
public class AppController {

    @Autowired
    private AppService appService;

    @PostMapping
    @ApiOperation(value = "添加应用", notes = "添加应用")
    @AuthRequired(auth = {AuthType.ORG})
    BaseResult<?> addNewApp(@ApiParam(value = "appVo", required = true) @Validated({AppVO.Submit.class}) @RequestBody AppVO appVo) throws Exception {
        return appService.add(appVo);
    }

    @PutMapping
    @ApiOperation(value = "编辑应用信息", notes = "编辑应用信息")
    @AuthRequired(auth = {AuthType.ORG})
    BaseResult<?> editAppInfo(@ApiParam(value = "应用信息", required = true) @Valid @RequestBody AppEditVO appVo) throws BusinessException {
        BaseResult<?> baseResult = appService.edit(appVo);
        return baseResult;
    }

    @ApiOperation(value = "查询应用列表", notes = "查询应用列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @AuthRequired(role = {RoleType.PLATFORM_MERCHANT, RoleType.ADMIN})
    BaseResult<?> queryAppList(Pageable pageable,
                               @ApiParam(value = "值") @RequestParam(value = "value", required = false) String value) throws Exception {
        return appService.queryAppList(pageable, value);
    }

    @GetMapping("{appId}")
    @ApiOperation(value = "查看应用信息", notes = "查看应用信息")
    @AuthRequired(auth = {AuthType.ORG})
    BaseResult<?> show(@ApiParam(value = "appId", required = true) @NotEmpty(message = "应用id不能为空") @PathVariable("appId") String appId) throws BusinessException {
        return appService.getAppInfo(appId);
    }

    @PostMapping("token")
    @ApiOperation(value = "申请token", notes = "申请token")
        //@AuthRequired(auth = {AuthType.ORG})
    BaseResult<?> getAccessToken(@ApiParam(value = "应用信息", required = true) @Valid @RequestBody AppTokenVO appTokenVo) {
        BaseResult<?> baseResult = appService.getAccessToken(appTokenVo.getAppKey(), appTokenVo.getAppSecret());
        return baseResult;
    }

    @DeleteMapping("/{appId}")
    @ApiOperation(value = "删除应用", notes = "删除应用")
    @AuthRequired(auth = {AuthType.ORG})
    BaseResult<?> deleteApp(@ApiParam(value = "appId", required = true) @NotEmpty(message = "应用id不能为空") @PathVariable("appId") String appId) {
        return appService.delete(appId);
    }

    @PostMapping("/draft")
    @ApiOperation(value = "保存应用草稿", notes = "保存应用草稿")
    @AuthRequired
    BaseResult<?> saveAppDraft(@ApiParam(value = "appVo", required = true) @Validated({Default.class}) @RequestBody AppVO appVo) {
        return appService.saveAppDraft(appVo);
    }

    @PutMapping("/toProd/{appId}")
    @ApiOperation(value = "应用转生产", notes = "应用转生产")
    @AuthRequired(auth = {AuthType.ORG})
    BaseResult<?> toProd(@ApiParam(value = "appId", required = true) @NotEmpty(message = "应用id不能为空") @PathVariable("appId") String appId) {
        return appService.toProd(appId);
    }

}
